﻿
namespace Apache.Cassandra.Client {
    using System;
    using System.ComponentModel;

    internal class CassandraConnectionString {

        private readonly object connectionString;

        private readonly string server;
        private readonly int port;
        private readonly string initialKeyspace;

        private readonly bool pooling;
        private readonly bool useDeactivateQueue;

        private readonly string performanceCountersInstance;

        internal CassandraConnectionString(object connectionString) {
            if (null == connectionString) {
                throw new ArgumentNullException("connectionString");
            }

            this.connectionString = connectionString;

            CassandraConnectionStringBuilder builder = new CassandraConnectionStringBuilder();
            string s = connectionString as string;

            if (null != s) {
                builder.ConnectionString = s;
            } else {
                foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(connectionString)) {
                    builder[property.Name] = property.GetValue(connectionString);
                }
            }

            server = builder.Server;
            port = builder.Port;
            initialKeyspace = builder.InitialKeyspace;

            pooling = builder.Pooling;
            useDeactivateQueue = builder.UseDeactivateQueue;

            performanceCountersInstance = builder.PerformanceCountersInstance;

            // TODO: Validate connection string properties
        }

        internal object ConnectionString {
            get {
                return connectionString;
            }
        }

        internal string Server {
            get {
                return server;
            }
        }

        internal int Port {
            get {
                return port;
            }
        }

        internal string InitialKeyspace {
            get {
                return initialKeyspace;
            }
        }

        internal bool Pooling {
            get {
                return pooling;
            }
        }

        internal bool UseDeactivateQueue {
            get {
                return useDeactivateQueue;
            }
        }

        internal string PerformanceCountersInstance {
            get {
                return performanceCountersInstance;
            }
        }
    }
}
